package cn.fancylab.template.web.action;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.fancylab.template.util.HttpUtil;
import cn.fancylab.template.util.JsonUtil;
import cn.fancylab.template.vo.ResultCode;
import cn.fancylab.web.support.FancyVelocityView;
import cn.fancylab.web.support.JsonStrDirectOut;

@Controller
public class ThirdPartServiceController extends AnnotationController {

	private String shopAppUrl = "http://localhost:6060/service/";

	// 获取企业列表
	@GetMapping("/thirdpartservice/getentpinfolist.xhtml")
	public String getEntpInfoList(String device_serialno, String gate_serialno, String custom_id, Integer type) {
		ResultCode resultCode = new ResultCode();

		StringBuffer url = new StringBuffer(shopAppUrl);
		url.append("WebService.do?method=getEntpInfoListByUserId&user_id=1");
		System.out.println(url.toString());

		HttpResponse respone = HttpUtil.executeGetMethod(url.toString());
		HttpEntity httpEntity = respone.getEntity();
		try {
			InputStream in = httpEntity.getContent();
			BufferedReader sf = new BufferedReader(new InputStreamReader(in));
			StringBuffer sb = new StringBuffer("");
			String s;
			while ((s = sf.readLine()) != null) {
				sb.append(s);
			}
			if (StringUtils.isNotBlank(sb)) {
				JSONObject jsonObject = JSON.parseObject(sb.toString());
				if (jsonObject.containsKey("datas")) {
					resultCode.setResultflag(true);
					resultCode.setResultData(jsonObject.getString("datas"));
				}
			}
		} catch (UnsupportedOperationException | IOException e) {
			e.printStackTrace();
		}

		HttpServletRequest request = getRequest();
		request.setAttribute(FancyVelocityView.RENDER_JSON, "true");
		request.setAttribute(FancyVelocityView.DIRECT_OUTPUT,
				new JsonStrDirectOut(getResponse(), JsonUtil.serialize(resultCode)));
		return "common/directOut.vm";
	}

	public static void main(String[] args) {
		StringBuffer url = new StringBuffer("http://localhost:6060/service/");
		url.append("WebService.do?method=getEntpInfoListByUserId&user_id=1");

		HttpResponse respone = HttpUtil.executeGetMethod(url.toString());
		HttpEntity httpEntity = respone.getEntity();
		try {
			InputStream in = httpEntity.getContent();
			BufferedReader sf = new BufferedReader(new InputStreamReader(in));
			StringBuffer sb = new StringBuffer("");
			String s;
			while ((s = sf.readLine()) != null) {
				sb.append(s);
			}
			System.out.println(sb);
			if (StringUtils.isNotBlank(sb)) {
				JSONObject jsonObject = JSON.parseObject(sb.toString());
				if (jsonObject.containsKey("datas")) {

				}
			}
		} catch (UnsupportedOperationException | IOException e) {
			e.printStackTrace();
		}
	}
}
